from typing import List

from appium.webdriver.common.appiumby import AppiumBy

_CONTACT_LIST = AppiumBy.XPATH, '//android.widget.TextView[@resource-id="com.tencent.wework:id/gje"]'


def add_contact(app, name, phone):
    result = (
        app
        .goto_main_page()
        .goto_contact()
        .goto_add_member_page()
        .goto_manual_input_page()
        .input_member_info(name, phone)
    )
    return type(result) is not str


def delete_contact(app, name):
    result = (
        app
        .goto_main_page()
        .goto_contact()
        .goto_contact_manage()
        .select_and_editor_contact(name)
        .del_member()
    )
    return type(result) is not str


def clear_contact_data(app):
    print("开始清理联系人数据============")
    manage_page = (
        app
        .goto_main_page()
        .goto_contact()
        .goto_contact_manage()

    )
    contact_els = manage_page.driver.find_elements(*_CONTACT_LIST)
    contacts = [contact_el.text for contact_el in contact_els]

    for contact in contacts:
        delete_contact(app, contact)
    print("清理联系人数据完成============")


def init_contact_data(app, contact_list: List[List[str]]):
    print("开始初始化联系人数据============")
    for contact in contact_list:
        add_contact(app, contact[0], contact[1])
    print("初始化联系人数据完成============")

